package com.ss.demo.springbootdemo.reflect;

import com.ss.demo.springbootdemo.entity.Person;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 作者: MrZhangJR
 * 时间: 2021/11/8 10:59
 * 描述:
 */
public class ReflectDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String classpath = sc.next();
        try {
            // 类对象
            Class<?> clazz = Class.forName(classpath);
            System.out.println("类对象创建完成");
            // 获取对象
            Person o = (Person) clazz.newInstance();
            // 拿到类方法
            String method = sc.next();
            Method setName = clazz.getMethod(method, String.class);
            // 执行方法
            setName.invoke(o, "zjr");
            String name = o.getName();
            System.out.println("name = " + name);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
            e.printStackTrace();
        }
    }
}
